Home

Row

#RMarkdown Tweets

30

#RMarkdown Retweets

110

Users

26

Likes

318

Row

Tweet volume

Tweets by Hour of Day

Row

💗 Most Liked Tweet Today

✨ Most Retweeted Tweet Today

🎉 Most Recent

Rankings

Row

Top Words

Top Locations

Row

Tweets in the current week

---
title: "RMarkdown Twitter Explorer"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    source_code: embed
    theme:
      version: 4
      bootswatch: yeti
    css: styles/main.css
---

```{r load_packages, include=FALSE}
devtools::load_all()
library(flexdashboard)
library(rtweet)
library(vroom)
library(dplyr)
library(lubridate)
library(echarts4r)
library(DT)
library(tidytext)
```

```{r load_data, include=FALSE, cache=TRUE}
rstats_tweets <- readData("data/tweets.csv.gz")
```

```{r process_data, include=FALSE, cache=TRUE}
timeline <- make_by_day_metrics(rstats_tweets)

n_tweets <- get_unique_value(rstats_tweets, text)

n_retweets <- rstats_tweets %>%
  pull(retweet_count) %>%
  sum()

n_users <- get_unique_value(rstats_tweets, user_id)

n_likes <- rstats_tweets %>%
  pull(favorite_count) %>%
  sum()

tweets_today <- rstats_tweets %>%
  filter(lubridate::date(created_at) == lubridate::today())

tweets_week <- rstats_tweets %>%
  filter(date(created_at) %within% interval(floor_date(today(), "week"), today()))

word_banlist <-  c("t.co", "https", "rmarkdown")
top_words <- rstats_tweets %>%
  select(text) %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words) %>%
  filter(!(word %in% word_banlist)) %>%
  filter(nchar(word) > 4) %>%
  count(word, sort = TRUE) %>%
  slice_max(n, n = 10, with_ties = FALSE) %>%
  select(word, n)

top_locations <- rstats_tweets %>%
  filter(!is.na(location) & location != "#rstats") %>%
  distinct(user_id, .keep_all = TRUE) %>%
  mutate(location = stringr::str_replace_all(location, "London$", "London, England")) %>%
  count(location) %>%
  slice_max(n, n = 10, with_ties = FALSE)

```

Home {data-icon="ion-home"}
====

Row
-------------------------------------

### #RMarkdown Tweets

```{r}
valueBox(n_tweets, icon = "fa-comments")
```

### #RMarkdown Retweets

```{r}
valueBox(n_retweets, icon = "fa-retweet")
```

### Users

```{r}
valueBox(n_users, icon = "fa-user")
```

### Likes

```{r}
valueBox(n_likes, icon = "fa-heart")
```

Row {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Tweet volume

```{r tweet_volume}
plot_tweet_volume(timeline)
```

### Tweets by Hour of Day

```{r tweets_by_hour}
plot_tweet_by_hour(rstats_tweets)
```

Row
-----------------------------------------------------------------------

### 💗 Most Liked Tweet Today {.tweet-box}

```{r most_liked}
most_liked_url <- tweets_today %>%
  slice_max(favorite_count, with_ties = FALSE)

get_tweet_embed(most_liked_url$screen_name, most_liked_url$status_id)
```

### ✨ Most Retweeted Tweet Today {.tweet-box}

```{r most_rt}
most_retweeted <- tweets_today %>%
  slice_max(retweet_count, with_ties = FALSE)

get_tweet_embed(most_retweeted$screen_name, most_retweeted$status_id)
```

### 🎉 Most Recent {.tweet-box}

```{r most_recent}
most_recent <- tweets_today %>%
  slice_max(created_at, with_ties = FALSE)

get_tweet_embed(most_recent$screen_name, most_recent$status_id)

```

Rankings {data-icon="ion-arrow-graph-up-right"}
=========

Row
-----------------------------------------------------------------------

### Top Words

```{r top_words}
top_words %>%
  e_charts(word) %>%
  e_bar(n, legend = FALSE) %>% 
  e_x_axis(
    axisLabel = list(
      interval = 0L,
      rotate = 30
    )
  ) %>%
  e_toolbox_feature("saveAsImage") %>%
  e_axis_labels(y = "Number of occurrences")
```

### Top Locations

```{r top_locations}
top_locations %>% 
	mutate(location = stringr::str_wrap(location, 9)) %>% 
  e_charts(location) %>% 
  e_bar(n, legend = FALSE) %>% 
  e_x_axis(
    axisLabel = list(
      interval = 0L,
      rotate = 30
    )
  ) %>%
  e_toolbox_feature("saveAsImage") %>%
  e_axis_labels(y = "Number of users from location")
```

Row
-----------------------------------------------------------------------

### Tweets in the current week {.datatable-container}

```{r datatable}
tweets_week %>%
  select(
    status_url,
    created_at,
    screen_name,
    text,
    retweet_count,
    favorite_count,
    mentions_screen_name
  ) %>%
  mutate(
    status_url = stringr::str_glue("On Twitter")
  ) %>%
  datatable(
    .,
    extensions = "Buttons",
    rownames = FALSE,
    escape = FALSE,
    colnames = c("Timestamp", "User", "Tweet", "RT", "Fav", "Mentioned"),
    filter = 'top',
    options = list(
      columnDefs = list(list(
        targets = 0, searchable = FALSE
      )),
      lengthMenu = c(5, 10, 25, 50, 100),
      pageLength = 10,
      scrollY = 600,
      scroller = TRUE,
      dom = '<"d-flex justify-content-between"lBf>rtip',
      buttons = list('copy', list(
        extend = 'collection',
        buttons = c('csv', 'excel'),
        text = 'Download'
      ))
    )
  )
```